package com.hex.ds.code.detection.common.config;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.hex.bigdata.hbda.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自动填充时间
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        try {
            this.setFieldValByName("createBy",  SecurityUtils.getUsername(), metaObject);
        } catch (Exception e) {
            this.setFieldValByName("createBy",  "admin", metaObject);
        }
        try {
            this.setFieldValByName("lastUpdateBy", SecurityUtils.getUsername(), metaObject);
        } catch (Exception e) {
            this.setFieldValByName("lastUpdateBy", "admin", metaObject);
        }
        if(ObjectUtil.isEmpty(this.getFieldValByName("createTime",metaObject))){
            this.setFieldValByName("createTime",  new Date(), metaObject);
        }
        if(ObjectUtil.isEmpty(this.getFieldValByName("lastUpdateTime",metaObject))){
            this.setFieldValByName("lastUpdateTime",  new Date(), metaObject);
        }
        this.setFieldValByName("del", "0", metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            this.setFieldValByName("lastUpdateBy", SecurityUtils.getUsername(), metaObject);
        } catch (Exception e) {
            this.setFieldValByName("lastUpdateBy", "admin", metaObject);
        }
        this.setFieldValByName("lastUpdateTime", new Date(), metaObject);

    }

}
